﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using InHD.model;
using InHD.dataaccess;
using System.Windows.Input;
using InHD.commands;
using System.Windows.Controls;
using InHD.utilities;
using System.Windows;
using InHD.Properties;

namespace InHD.viewmodels
{
    class EditProfileViewModel : WorkspaceViewModel, IDataErrorInfo
    {
        #region Fields
        private Staff _staff;
        readonly StaffRepository _staffRepository;
        #endregion

        public EditProfileViewModel()
        {
            this._staffRepository = new StaffRepository();
            this._staffRepository.LoadAllStaffs();
        }

        public EditProfileViewModel(Staff staff)
        {
            this._staff = staff;
            this._staffRepository = new StaffRepository();
            this._staffRepository.LoadAllStaffs();
        }

        #region Properties
        public string StaffCode
        {
            get { return this._staff.StaffCode; }
        }

        public string Username
        {
            get { return this._staff.Username; }
        }

        public string SType
        {
            get { return this._staff.SType.ToString(); }
        }

        public string Fullname
        {
            get { return this._staff.Fullname; }
            set
            {
                if (value == _staff.Fullname)
                    return;

                _staff.Fullname = value;

                base.OnPropertyChanged("Fullname");
            }
        }

        public string Phone
        {
            get { return this._staff.Phone; }
            set
            {
                if (value == _staff.Phone)
                    return;

                _staff.Phone = value;

                base.OnPropertyChanged("Phone");
            }
        }

        public string Email
        {
            get { return this._staff.Email; }
            set
            {
                if (value == _staff.Email)
                    return;

                _staff.Email = value;

                base.OnPropertyChanged("Email");
            }
        }

        public string Address
        {
            get { return this._staff.Address; }
            set
            {
                if (value == _staff.Address)
                    return;

                _staff.Address = value;

                base.OnPropertyChanged("Address");
            }
        }

        public Staff CurrentStaff
        {
            get
            {
                return this._staff;
            }

            set
            {
                this._staff = value;
            }
        }
        #endregion

        #region Commmands
        private RelayCommand _saveCommand;
        public ICommand SaveCommand
        {
            get
            {
                if (_saveCommand == null)
                {
                    _saveCommand = new RelayCommand(
                        (param) => this.Save(param),
                        (param) => this.CanSave
                        );
                }
                return _saveCommand;
            }
        }
        #endregion

        #region Private helpers
        private void Save(object param)
        {
            //// Check in staff repository
            PasswordParameters values = (PasswordParameters)param;
            string currentPwd = values.CurrentPwd.Password;
            bool changePwd = values.ChangePwd;
            string newPwd = values.NewPwd.Password;
            string confirmPwd = values.ConfirmPwd.Password;

            if (string.IsNullOrEmpty(currentPwd))
            {
                MessageBox.Show(Resources.Profile_EnterPwd);
                return;
            }

            if (!currentPwd.Equals(_staff.Password))
            {
                MessageBox.Show(Resources.Profile_IncorrectPwd);
                return;
            }

            if (changePwd)
            {
                if (string.IsNullOrEmpty(newPwd))
                {
                    MessageBox.Show(Resources.Profile_EnterNewPwd);
                    return;
                }

                if (string.IsNullOrEmpty(confirmPwd))
                {
                    MessageBox.Show(Resources.Profile_EnterConfirmPwd);
                    return;
                }

                if (!newPwd.Equals(confirmPwd))
                {
                    MessageBox.Show(Resources.Profile_EnterSamePwd);
                    return;
                }

                if (currentPwd.Equals(newPwd))
                {
                    MessageBox.Show(Resources.Profile_EnterDifferentPwd);
                    return;
                }

                this._staff.Password = newPwd;
            }

            if (string.IsNullOrEmpty(_staffRepository.EditStaff(_staff)))
            {
                OnRequestClose();
            }
            else
            {
                MessageBox.Show(Resources.InHD_DB_Error);
            }
        }

        private bool CanSave { get { return true; } }
        #endregion

        #region IDataErrorInfo Members

        string IDataErrorInfo.Error
        {
            get { return (_staff as IDataErrorInfo).Error; }
        }

        string IDataErrorInfo.this[string propertyName]
        {
            get
            {
                string error = null;


                error = (_staff as IDataErrorInfo)[propertyName];


                // Dirty the commands registered with CommandManager,
                // such as our Save command, so that they are queried
                // to see if they can execute now.
                CommandManager.InvalidateRequerySuggested();

                return error;
            }
        }

        #endregion // IDataErrorInfo Members

        public override string DisplayName
        {
            get
            {
                try
                {
                    return Resources.Title_Profile;
                }
                catch (Exception ex)
                {
                    return string.Empty;
                }
            }

        }
    }
}
